package cn.sxt.yxl.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.users.dao.IAAdminsDao;
import cn.sxt.yxl.users.dao.IARoleDao;
import cn.sxt.yxl.users.pojo.AAdmins;
import cn.sxt.yxl.users.pojo.ARole;
import cn.sxt.yxl.users.service.IUsersService;

@Service("usersService")
public class UsersServiceImpl implements IUsersService {
	
	@Resource
	IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;
	@Override
	public AAdmins findOneAdmins(Map<String, Object> condMap) {
		return this.adminsDao.findOne(condMap);
	}
	@Override
	public JSONObject saveOneAdmins(AAdmins admins) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.adminsDao.saveOne(admins);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","save success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","save failure");
		}
		return resultJSON;
	}
	@Override
	public JSONObject updateOneAdmins(AAdmins admins) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.adminsDao.updateOne(admins);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","update success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","update failure");
		}
		return resultJSON;
	}
	@Override
	public JSONObject deleteOneAdmins(AAdmins admins) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.adminsDao.deleteOne(admins);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","delete success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","delete failure");
		}
		return resultJSON;
	}
	@Override
	public ARole findOneARole(Map<String, Object> condMap) {
		return this.roleDao.findOne(condMap);
	}
	@Override
	public JSONObject saveOneARole(ARole role) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.roleDao.saveOne(role);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","save success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","save failure");
		}
		return resultJSON;
	}
	@Override
	public JSONObject updateOneARole(ARole role) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.roleDao.updateOne(role);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","update success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","update failure");
		}
		return resultJSON;
	}
	@Override
	public JSONObject deleteOneARole(ARole role) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.roleDao.deleteOne(role);
		if(res > 0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("res", res);
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","delete success");
		}else {
			resultJSON.put("id", "1");
			resultJSON.put("info","delete failure");
		}
		return resultJSON;
	}
	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null) {
			Page<AAdmins> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AAdmins> adminsList = this.adminsDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));

			return adminsList;
		}
		return this.adminsDao.findCondList(condMap);
	}
	@Override
	public List<ARole> findCondListARoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null) {
			Page<ARole> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<ARole> roleList = this.roleDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return roleList;
		}
		return this.roleDao.findCondList(condMap);
	}
}
